﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CoreHelper.Validation.Validators
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class RegexValidatorAttribute : BaseValidatorAttribute
    {

        public RegexValidatorAttribute(string pattern) : this(pattern, ValidationUtils.Messages.Regex)
        {
        }

        public RegexValidatorAttribute(string pattern, string errorMessage) : base(errorMessage)
        {
            this.Pattern = pattern;
        }

        public string Pattern { get; protected set; }

        public override bool IsValid(object value)
        {
            if (value == null) return false;
            return Regex.IsMatch(value.ToString(), Pattern, RegexOptions.IgnoreCase | RegexOptions.ECMAScript);
        }
    }
}
